﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using RIO.Utilits;
using SpeechLib;
using RIO.Rooms;
using gma.System.Windows;
using System.Diagnostics;

using System.Threading;

namespace RIO
{

    public partial class frmMain : Form
    {
        Thread hotKeyThread;

        short _hotKey1 = GlobalHotKey.GlobalAddAtom("RIOHotKey1");
        short _hotKey2 = GlobalHotKey.GlobalAddAtom("RIOHotKey2");
        short _hotKey3 = GlobalHotKey.GlobalAddAtom("RIOHotKey3");

        BaseRoom _room = new BaseRoom();

        public frmMain()
        {
            InitializeComponent();

            //GlobalHotKey.RegisterHotKey(this.Handle, _hotKey1, (uint)Keys.None, (uint)Keys.F9);
            //GlobalHotKey.RegisterHotKey(this.Handle, _hotKey2, (uint)Keys.None, (uint)Keys.F10);
            //GlobalHotKey.RegisterHotKey(this.Handle, _hotKey3, (uint)Keys.None, (uint)Keys.F11);

            var hook = new UserActivityHook(false, true);
            hook.KeyDown += new KeyEventHandler(hook_KeyUp);
            
        }

        private void hook_KeyUp(object sender, KeyEventArgs e)
        {
            if (hotKeyThread != null && hotKeyThread.IsAlive)
            {
                return;
            }

            ThreadStart action;
            switch (e.KeyData)
            {
                case Keys.F9:
                    action = _room.GetHint;
                   break;
                case Keys.F10:
                    action = _room.GetNextHint;
                    break;
                case Keys.F11:
                    action = _room.SaveScreenshot;
                    break;
                case Keys.F12:
                    action = _room.ShowDebugScreen;
                    break;
                default:
                    return;
            }
            
            hotKeyThread = new Thread(action);
            hotKeyThread.Start();
        }

        protected override void WndProc(ref Message m)
        {
            Debug.WriteLine(m.WParam);
            Debug.WriteLine("-");
            if (m.Msg == GlobalHotKey.WM_HOTKEY && m.WParam == (IntPtr)_hotKey1)
            {
                _room.GetHint();
            }
            else if (m.Msg == GlobalHotKey.WM_HOTKEY && m.WParam == (IntPtr)_hotKey2)
            {
                _room.GetNextHint();
            }
            else if (m.Msg == GlobalHotKey.WM_HOTKEY && m.WParam == (IntPtr)_hotKey3)
            {
                //_boardInfo.SaveScreenshot();
                _room.SaveScreenshot();
            }
            base.WndProc(ref m);
        }

        private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            GlobalHotKey.UnregisterHotKey(this.Handle, _hotKey1);
            GlobalHotKey.UnregisterHotKey(this.Handle, _hotKey2);
            GlobalHotKey.UnregisterHotKey(this.Handle, _hotKey3);
            GlobalHotKey.GlobalDeleteAtom(_hotKey1);
            GlobalHotKey.GlobalDeleteAtom(_hotKey2);
            GlobalHotKey.GlobalDeleteAtom(_hotKey3);

            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            BaseRoom br = new BaseRoom();
            br.GetHint();

        }
    }
}
